using System;
using System.Collections.Generic;
using System.Text;
using BusinessObjects;
using System.Management;
using System.Data;

namespace GUI
{
    public class WMIManager
    {
        public List<Connection> GetConnections()
        {
            List<Connection> insListConnection = new List<Connection>();
            ManagementObjectSearcher m = new ManagementObjectSearcher();
            m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True");

            foreach (ManagementObject mo in m.Get())
            {
                Connection c = new Connection();
                c.ConnectionId = mo["Index"].ToString();
                c.ConnectionName = mo["Caption"].ToString().Substring(mo["Caption"].ToString().IndexOf("]") + 1).Trim();
                insListConnection.Add(c);
            }
            return insListConnection;
        }
        public void SetConnectionProfile(Connection c, Profile p)
        {
            ManagementObjectSearcher m = new ManagementObjectSearcher();
            m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True");
            foreach (ManagementObject mo in m.Get())
            {
                if (mo["Index"].ToString()==c.ConnectionId)
                {
                    mo.InvokeMethod("EnableStatic", new object[] { new string[] { p.IpAddress }, new string[] { p.SubnetMask } });
                    mo.InvokeMethod("SetGateways", new object[] { new string[] { p.DefaultGateway }, new string[] { "1" } });
                    mo.InvokeMethod("SetDNSServerSearchOrder", new object[] { new string[] { p.PreferredDns} });
                    if (p.ProxyEnabled)
                    {
                        RegistryManager.EnableProxy = true;
                        RegistryManager.BypassProxyForLocalAddresses = p.ByPassProxyForLocal;
                        RegistryManager.ProxyAddress= p.ProxyAddress + ":" + p.ProxyPort;
                    }
                    else
                    {
                        RegistryManager.EnableProxy = false;
                        RegistryManager.ProxyAddress = "";
                        RegistryManager.BypassProxyForLocalAddresses = true;
                    }
                }
            }
        }
        public void SetConnectionAutomatic(Connection c)
        {
            ManagementObjectSearcher m = new ManagementObjectSearcher();
            m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True");
            foreach (ManagementObject mo in m.Get())
            {
                if (mo["Index"].ToString() == c.ConnectionId)
                {
                    mo.InvokeMethod("EnableDHCP", null);
                    mo.InvokeMethod("SetDNSServerSearchOrder", null);
                    RegistryManager.EnableProxy = false;
                    RegistryManager.ProxyAddress = "";
                    RegistryManager.BypassProxyForLocalAddresses = true;
                }
            }
        }
    }
}
